package com.leo;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Label;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

import java.net.URL;
import java.util.function.Consumer;

/**
 * @ClassName BarChartDemo
 * @Description
 * @Author wangss
 * @date 2020.06.28 23:49
 * @Version 1.0
 */
public class BarChartDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        BarChart<String, Number> barChart = createChart();
        BarChart<String, Number> barChart1 = createChart1();
        BarChart<String, Number> barChart2 = createChart2();
        BarChart<Number, String> barChart3 = createChart3();

        HBox hBox = new HBox(10);
        hBox.getChildren().addAll(barChart, barChart1, barChart2, barChart3);

        AnchorPane root = new AnchorPane();
        root.getChildren().addAll(hBox);
        AnchorPane.setTopAnchor(hBox, 100d);

        Scene scene = new Scene(root);
        URL url = getClass().getResource("/css/mycss.css");
        scene.getStylesheets().add(url.toExternalForm());

        primaryStage.setScene(scene);
        primaryStage.setTitle("位移");
        primaryStage.setHeight(800d);
        primaryStage.setWidth(800d);
        primaryStage.show();
    }

    private BarChart<String, Number> createChart() {
        CategoryAxis x = new CategoryAxis();
        x.setLabel("国家");

//        x.setStartMargin(100); // 距离左边距离
//        x.setEndMargin(100); // 距离右边距离
//        x.setGapStartAndEnd(true); // 前后留空间

//        x.setSide(Side.TOP); // 轴位置
//        x.setTickLabelFill(Paint.valueOf("#ff55ff"));
//        x.setTickLabelFont(new Font(16));
//        x.setTickLabelGap(0); // 类型名称与轴刻度的距离
//        x.setTickLabelRotation(10); // 类型名称旋转
//        x.setTickLabelsVisible(true); // 是否显示名称
//        x.setTickLength(100); // 刻度长度
//        x.setTickMarkVisible(false); // 是否显示刻度


        NumberAxis y = new NumberAxis(0, 100, 10);
        y.setLabel("生产值");

        ObservableList<XYChart.Series<String, Number>> list = FXCollections.observableArrayList();
        XYChart.Series<String, Number> xy = new XYChart.Series<>();
        xy.setName("生产总值");

        ObservableList<XYChart.Data<String, Number>> dataList = FXCollections.observableArrayList();
        XYChart.Data<String, Number> d1 = new XYChart.Data<>("中国", 80);
        XYChart.Data<String, Number> d2 = new XYChart.Data<>("美国", 90);
        XYChart.Data<String, Number> d3 = new XYChart.Data<>("日本", 25);
        dataList.addAll(d1, d2, d3);

        xy.setData(dataList);
        list.addAll(xy);

        BarChart<String, Number> barChart = new BarChart<>(x, y, list);

//        barChart.setAnimated(true);
//        barChart.setLegendSide(Side.LEFT);
//        barChart.setLegendVisible(false);
//        barChart.setTitleSide(Side.LEFT);

//        barChart.setBarGap(40);
//        barChart.setCategoryGap(10);
//        barChart.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);

        return barChart;
    }

    private BarChart<String, Number> createChart1() {
        CategoryAxis x = new CategoryAxis();
        x.setLabel("国家");

        NumberAxis y = new NumberAxis(0, 100, 10);
        y.setLabel("生产值");

        XYChart.Series<String, Number> xy = new XYChart.Series<>();
        xy.setName("中国");

        XYChart.Series<String, Number> xy1 = new XYChart.Series<>();
        xy1.setName("美国");

        XYChart.Series<String, Number> xy2 = new XYChart.Series<>();
        xy2.setName("日本");

        XYChart.Data<String, Number> d1 = new XYChart.Data<>("GDP", 80);
        XYChart.Data<String, Number> d2 = new XYChart.Data<>("GDP", 90);
        XYChart.Data<String, Number> d3 = new XYChart.Data<>("GDP", 25);

        XYChart.Data<String, Number> d4 = new XYChart.Data<>("GNP", 80);
        XYChart.Data<String, Number> d5 = new XYChart.Data<>("GNP", 90);
        XYChart.Data<String, Number> d6 = new XYChart.Data<>("GNP", 25);

        xy.getData().addAll(d1, d4);
        xy1.getData().addAll(d2, d5);
        xy2.getData().addAll(d3, d6);

        BarChart<String, Number> barChart = new BarChart<>(x, y);
        barChart.getData().addAll(xy, xy1, xy2);

        return barChart;
    }

    private BarChart<String, Number> createChart2() {
        CategoryAxis x = new CategoryAxis();
        x.setLabel("国家");

        NumberAxis y = new NumberAxis(0, 100, 10);
        y.setLabel("生产值");

        XYChart.Series<String, Number> xy = new XYChart.Series<>();
        xy.setName("GDP");

        XYChart.Series<String, Number> xy1 = new XYChart.Series<>();
        xy1.setName("GNP");

        XYChart.Data<String, Number> d1 = new XYChart.Data<>("中国", 80);
        XYChart.Data<String, Number> d2 = new XYChart.Data<>("美国", 90);
        XYChart.Data<String, Number> d3 = new XYChart.Data<>("日本", 25);

        XYChart.Data<String, Number> d4 = new XYChart.Data<>("中国", 80);
        XYChart.Data<String, Number> d5 = new XYChart.Data<>("美国", 90);
        XYChart.Data<String, Number> d6 = new XYChart.Data<>("日本", 25);

        xy.getData().addAll(d1, d2, d3);
        xy1.getData().addAll(d4, d5, d6);

        xy.getData().forEach(new Consumer<XYChart.Data<String, Number>>() {
            @Override
            public void accept(XYChart.Data<String, Number> stringNumberData) {
                HBox h1 = new HBox();
                h1.setAlignment(Pos.CENTER);
                h1.setStyle("-fx-background-color: #eea5c4");
                h1.getChildren().add(new Label(String.valueOf(stringNumberData.getYValue())));
                stringNumberData.setNode(h1);

                stringNumberData.getNode().setOnMouseClicked(new EventHandler<MouseEvent>() {
                    @Override
                    public void handle(MouseEvent event) {
                        System.out.println(stringNumberData.getXValue());
                    }
                });
            }
        });

        BarChart<String, Number> barChart = new BarChart<>(x, y);
        barChart.getData().addAll(xy, xy1);

        return barChart;
    }

    private BarChart<Number, String> createChart3() {
        CategoryAxis x = new CategoryAxis();
        x.setLabel("国家");

        NumberAxis y = new NumberAxis(0, 100, 10);
        y.setLabel("生产值");

        XYChart.Series<Number, String> xy = new XYChart.Series<>();
        xy.setName("GDP");

        XYChart.Series<Number, String> xy1 = new XYChart.Series<>();
        xy1.setName("GNP");

        XYChart.Data<Number, String> d1 = new XYChart.Data<>(80, "中国");
        XYChart.Data<Number, String> d2 = new XYChart.Data<>(90, "美国");
        XYChart.Data<Number, String> d3 = new XYChart.Data<>(25, "日本");

        XYChart.Data<Number, String> d4 = new XYChart.Data<>(80, "中国");
        XYChart.Data<Number, String> d5 = new XYChart.Data<>(90, "美国");
        XYChart.Data<Number, String> d6 = new XYChart.Data<>(25, "日本");

        xy.getData().addAll(d1, d2, d3);
        xy1.getData().addAll(d4, d5, d6);

        BarChart<Number, String> barChart = new BarChart<>(y, x);
        barChart.getData().addAll(xy, xy1);

        return barChart;
    }
}
